package com.qf.web.controller;/*
*王浩政 
*
2022/8/21
15:28
*小米商城
*/

import com.qf.service.UserService;
import com.qf.service.UserServiceImpl;
import com.qf.utils.Base64Utils;
import com.qf.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/activate")
public class ActivateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String email = req.getParameter("e");
        String code = req.getParameter("c");
        //校验
        if (StringUtil.isEmpty(email)||StringUtil.isEmpty(code)){
            req.setAttribute("msg","激活失败");
            req.getRequestDispatcher("/message.jsp").forward(req,resp);
            return;

        }
        email= Base64Utils.decode(email);
        code=Base64Utils.decode(code);
        //激活
        UserService userService=new UserServiceImpl();
        try {
            userService.active(email,code);
            req.setAttribute("msg","激活成功");
            req.getRequestDispatcher("/message.jsp").forward(req,resp);
        } catch (Exception e) {
            req.setAttribute("msg","激活失败"+e.getMessage());
            req.getRequestDispatcher("/message.jsp").forward(req,resp);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
